//
//  DownCollectionViewCell.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "DownCollectionViewCell.h"

@implementation DownCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createTableView];
        
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    
    
    
}

- (void)setUrl:(NSString *)url {
    if (_url != url) {
        _url = url;
    }
    [self getNetData];
    
}

//进行网络请求

- (void)getNetData {
    self.dataSource = [NSMutableArray array];
    [WXCNetWorkTool getWithUrl:self.url parameter:nil httpHeader:nil responseType:ResponseTypeJson success:^(id result) {
        for (NSDictionary *dic in result[@"list"]) {
            DownModel *downModel = [[DownModel alloc] initWithDictionAry:dic];
            [self.dataSource addObject:downModel];
        }
        
        [self.tableView reloadData];

    } fail:^(NSError *error) {
        
        
    }];
    
}

//点击用block传值
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //传一个albumid过去
    self.block([self.dataSource[indexPath.row] albumId]);
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DownTableViewCellIdentifier"];

    
    cell.downModel = self.dataSource[indexPath.row];
    return cell;
}


- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[DownTableViewCell class] forCellReuseIdentifier:@"DownTableViewCellIdentifier"];
    [self addSubview:self.tableView];
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}




@end
